﻿using OpenHardwareMonitor.Hardware;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Timers;
using System.Windows;
using System.Windows.Forms;
using Application = System.Windows.Application;

namespace NASA505TM
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        /// <summary>
        /// 程序标题
        /// </summary>
        public static string TitleInfo = "NASA505专用系统温度监控";
        /// <summary>
        /// 托盘图标
        /// </summary>
        private static NotifyIcon trayIcon;
        /// <summary>
        /// PC硬件对象
        /// </summary>
        private static Computer computer;
        /// <summary>
        /// 定时读取温度时钟
        /// </summary>
        private System.Timers.Timer TempChecker;

        public double TempCheckerInterval { get; set; }
        /// <summary>
        /// CPU温度
        /// </summary>
        public static float CPUTemp { get; set; }
        /// <summary>
        /// 显卡温度
        /// </summary>
        public static float GPUTemp { get; set; }
        /// <summary>
        /// 气泡信息
        /// </summary>
        public static string TipMsg { get { return string.Format("{0}\r\nCPU:{1}\r\nGPU:{2}", TitleInfo, CPUTemp, GPUTemp); } }

        /// <summary>
        /// 开始运行的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ApplicationStartup(object sender, StartupEventArgs e)
        {
            TempCheckerInterval = TimeSpan.FromSeconds(1).TotalMilliseconds;
            InitTempChecker();
            InitNotifyIcon();
        }
        /// <summary>
        /// 初始化计时器
        /// </summary>
        private void InitTempChecker()
        {
            TempChecker = new System.Timers.Timer
            {
                Interval = TempCheckerInterval
            };
            
            TempChecker.Elapsed += TempCheckerElapsedAction;
            TempChecker.Start();
        }
        private int viewIndex = 0;
        private void TempCheckerElapsedAction(object sender, ElapsedEventArgs e)
        {
            viewIndex++;
            if (viewIndex == 2)
                CreateIcon(CPUTemp, Brushes.Green);
            if (viewIndex == 4)
                CreateIcon(GPUTemp, Brushes.White);
            if (viewIndex >= 4)
            {
                RefreshHardwareInfo();
                viewIndex = 0;
            }
        }

        static void RefreshHardwareInfo()
        {
            if (null == computer)
                computer = new Computer();
            computer.Open();
            computer.GPUEnabled = true;
            computer.CPUEnabled = true;
            computer.Accept(new UpdateVisitor());
            foreach (var HardwareItem in computer.Hardware)
            {
                switch (HardwareItem.HardwareType)
                {
                    case HardwareType.CPU:
                        foreach (var SubItem in HardwareItem.Sensors)
                        {
                            if (SubItem.Name.Equals("CPU Total"))
                                CPUTemp = SubItem.Value ?? 0;
                        }
                        break;
                    case HardwareType.GpuAti:
                        foreach (var SubItem in HardwareItem.Sensors)
                        {
                            switch (SubItem.SensorType)
                            {
                                case SensorType.Temperature:
                                    GPUTemp = SubItem.Value ?? 0;
                                    break;
                            }
                        }
                        break;
                    case HardwareType.GpuNvidia:
                        foreach (var SubItem in HardwareItem.Sensors)
                        {
                            switch (SubItem.SensorType)
                            {
                                case SensorType.Temperature:
                                    GPUTemp = SubItem.Value ?? 0;
                                    break;
                            }
                        }
                        break;
                }
            }
            computer.Close();
            trayIcon.Text = TipMsg;
        }

        private static void CreateIcon(float text,Brush color)
        {
            if (trayIcon.Icon != null) 
                trayIcon.Icon.Dispose();
            Bitmap _Bitmap = new Bitmap(32, 32);
            Graphics _Graphics = Graphics.FromImage(_Bitmap);
            _Graphics.DrawString(text.ToString().PadLeft(2, '0'), new Font("黑体", 20), color, new System.Drawing.Point(0, 0));
            _Graphics.Dispose();
            trayIcon.Icon = Icon.FromHandle(_Bitmap.GetHicon());
        }

        /// <summary>
        /// 添加基础信息
        /// </summary>
        private void InitNotifyIcon()
        {
            if (trayIcon != null)
            {
                return;
            }
            trayIcon = new NotifyIcon
            {
                Visible = true
            };

            #region 添加右键菜单内容
            MenuItem OnlineItem = new MenuItem
            {
                Text = "退出"
            };
            OnlineItem.Click += Exit_Click;

            ContextMenu menu = new ContextMenu();
            menu.MenuItems.Add(OnlineItem);
            trayIcon.ContextMenu = menu;//设置右键弹出菜单
            #endregion
        }
        /// <summary>
        /// 退出事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Exit_Click(object sender, EventArgs e)
        {
            ApplicationExit(null, null);
        }

        /// <summary>
        /// 结束时调用的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ApplicationExit(object sender, ExitEventArgs e)
        {
            ClearTimerChecker();
            ClearTrayIcon();
            Process.GetCurrentProcess().Kill();
        }

        private void ClearTimerChecker()
        {
            if (null != computer)
            {
                computer.Close();
                computer = null;
            }
            if (null != TempChecker)
            {
                TempChecker.Stop();
                TempChecker.Dispose();
                TempChecker = null;
            }
        }
        /// <summary>
        /// 释放资源
        /// </summary>
        private void ClearTrayIcon()
        {
            if (trayIcon != null)
            {
                trayIcon.Visible = false;
                trayIcon.Dispose();//释放资源
                trayIcon = null;
            }
        }
    }
   
}
